const { DataTypes } = require('sequelize');
const { sequelize } = require('../config/database');

const StoredWine = sequelize.define('StoredWine', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  userId: {
    type: DataTypes.INTEGER,
    allowNull: false,
    field: 'user_id',
    references: {
      model: 'users',
      key: 'id'
    }
  },
  wineName: {
    type: DataTypes.STRING(100),
    allowNull: false,
    field: 'wine_name'
  },
  wineType: {
    type: DataTypes.STRING(50),
    allowNull: false,
    field: 'wine_type'
  },
  quantity: {
    type: DataTypes.DECIMAL(10, 2),
    allowNull: false,
    validate: {
      min: 0
    }
  },
  unit: {
    type: DataTypes.STRING(20),
    defaultValue: '瓶'
  },
  storageDate: {
    type: DataTypes.DATEONLY,
    allowNull: false,
    field: 'storage_date'
  },
  notes: {
    type: DataTypes.TEXT,
    allowNull: true
  },
  status: {
    type: DataTypes.ENUM('stored', 'taken'),
    defaultValue: 'stored'
  },
  createdAt: {
    type: DataTypes.DATE,
    field: 'created_at',
    defaultValue: DataTypes.NOW
  },
  updatedAt: {
    type: DataTypes.DATE,
    field: 'updated_at',
    defaultValue: DataTypes.NOW
  }
}, {
  tableName: 'stored_wines',
  timestamps: true,
  indexes: [
    {
      fields: ['user_id']
    },
    {
      fields: ['status']
    }
  ]
});

module.exports = StoredWine;
